package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.starter.utils.RandomUtil;
import com.zis.starter.utils.SecurityUtil;
import com.zis.system.dao.UserDao;
import com.zis.system.inter.IUserInter;
import com.zis.system.model.User;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/1/31
 */
@Service
@Transactional("txManager")
public class UserInterImpl implements IUserInter {

    @Autowired
    private UserDao userDao;

    @Override
    public ResponseEntity insert(User model) throws Exception {
        String slat = RandomUtil.getRandomStr(16);//16位密码校验码
        String password = SecurityUtil.AESEncode(slat, Constant.DEFAULT_PASSWORD);//密码加密
        model.setSalt(slat);
        model.setPassword(password);
        int num = userDao.getSQLManager().insert(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity update(User model) throws Exception {
        int num = userDao.getSQLManager().updateTemplateById(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        int num = userDao.getSQLManager().deleteById(User.class, id);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        User result = userDao.unique(id);
        if (result != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(result)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    @Override
    public PageQuery<User> findByPage(SearchParams params) throws Exception {
        PageQuery<User> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        userDao.getSQLManager().pageQuery("system.user.query", User.class, query);
        return query;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        List<User> resultList = userDao.getSQLManager().select("system.user.query", User.class, params);
        if (resultList != null && resultList.size() > 0) {
            return ResponseEntity.success(JSON.parseArray(JSON.toJSONString(resultList)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 根据用户名查询用户信息
     * @param loginName
     * @return 存在返回json字符串，否则null
     * @throws Exception
     */
    @Override
    public ResponseEntity findByLoginName(String loginName) throws Exception {
        User user = userDao.findByLoginName(loginName);
        if (user != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(user)));
        }
        return ResponseEntity.failure("null");
    }
}
